<?php
class TaskManager extends CI_Model {
	public function __construct()
	{
		$this->load->model('Util');
		$this->load->model('SessionManager');
		$this->load->model('UserManager');
		$this->load->model('DatabaseManager');
		parent::__construct();
	}
	public function get_task_list()
	{
		$data=$this->DatabaseManager->get_task_list();
		return $this->Util->format_return(1,1,'success',$data);;
	}
	public function get_task_list_by_username($username,$sessionId)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_task_list_by_username($username);
		return $this->Util->format_return(1,1,'success',$data);
	}
	public function get_receive_task_list($username,$sessionId)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_task_list_by_receive_user($username);
		return $this->Util->format_return(1,1,'success',$data);
	}
	public function release_task($username,$sessionId,$data)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$row=json_decode($data,true);
		if($row['id']==null){
			$this->DatabaseManager->release_task($username,$row);
			return $this->Util->format_return(1,1,'success',null);
		}else{
			unset($row['isReceived']);
			$data=$this->DatabaseManager->get_task_by_id($row['id']);
			if($data['id']==null){
				return $this->Util->format_return(0,10,'task is delete',null);
			}else {
				$this->DatabaseManager->edit_task($row);
				return $this->Util->format_return(1,1,'success',null);
			}
		}
	}
	public function receive_task($username,$sessionId,$id)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_task_by_id($id);
		if($data['id']==null){
			return $this->Util->format_return(0,10,'task is delete',null);
		}
		$data=$this->DatabaseManager->get_task_by_id($id);
		$receiveUser=$data['receiveUser'];
		if($status!=0&&$receiveUser!=null){
			return $this->Util->format_return(0,11,'task is received',$this->format_taskDetail($username,$sessionId,$data));
		}
		if($this->DatabaseManager->receive_task($username,$id)){
			$data=$this->DatabaseManager->get_task_by_id($id);
			$this->UserManager->add_friend($username,$sessionId,$data['username']);
			return $this->Util->format_return(1,1,'success',$this->format_taskDetail($username,$sessionId,$data));
		}

	}
	public function finish_task($username,$sessionId,$id)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_task_by_id($id);
		if($data['id']==null){
			return $this->Util->format_return(0,10,'task is delete',null);
		}
		$data=$this->DatabaseManager->get_task_by_id($id);
		$status=$data['status'];
		if($status!=1){
			return $this->Util->format_return(0,12,'task is not received',$this->format_taskDetail($username,$sessionId,$data));
		}
		if($this->DatabaseManager->finish_task($id)){
			$data=$this->DatabaseManager->get_task_by_id($id);
			return $this->Util->format_return(1,1,'success',$this->format_taskDetail($username,$sessionId,$data));
		}
	}
	public function get_task_detail($username,$sessionId,$id)
	{
		$data=$this->DatabaseManager->get_task_by_id($id);
		if($data['id']==null){
			return $this->Util->format_return(0,10,'task is delete',null);
		}
		return $this->Util->format_return(1,1,'success',$this->format_taskDetail($username,$sessionId,$data));
	}
	public function evalute_task($username,$sessionId,$id,$rating){
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_task_by_id($id);
		if($data['id']==null){
			return $this->Util->format_return(0,10,'task is delete',null);
		}
		if($username!=$data['username']){
			return $this->Util->format_return(0,14,'task user error',null);
		}
		if($data['status']!=2){
			return $this->Util->format_return(0,15,'not to evalute',$this->format_taskDetail($username,$sessionId,$data));
		}
		$this->DatabaseManager->close_task($id);
		$receiveUser=$data['receiveUser'];
		$this->DatabaseManager->add_integral_detail($receiveUser,"帮助奖励5+评分".$rating,0,(5+$rating),time());
		$this->DatabaseManager->add_integral($receiveUser,$rating+5);
		$data=array_merge($data,array('status'=>3));
		return $this->Util->format_return(1,1,'success',$this->format_taskDetail($username,$sessionId,$data));
	}
	public function delete_task($username,$sessionId,$id){
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_task_by_id($id);
		if($data['id']==null){
			return $this->Util->format_return(0,10,'task is delete',null);
		}
		if($username!=$data['username']){
			return $this->Util->format_return(0,14,'task user error',null);
		}
		if($data['status']!=0){
			return $this->Util->format_return(0,16,'forbidden',$this->format_taskDetail($username,$sessionId,$data));
		}
		$this->DatabaseManager->delete_task($username,$id);
		return $this->Util->format_return(1,1,'success',null);
	}
	private function format_taskDetail($username,$sessionId,$data){
		$releaseUser=$data['username'];
		$receiveUser=$data['receiveUser'];
		$status=$data['status'];
		if($releaseUser==$username||$receiveUser==$username){
			if($this->SessionManager->check($username,$sessionId)){
				$data=array_merge($data,array('isReceived'=>true,'remark'=>base64_decode($data['remark'])));
				if($status!=0&&$receiveUser!=null&&$releaseUser==$username){
					$user=$this->DatabaseManager->get_user_info($receiveUser);
					$data=array_merge($data,array('receiveUser'=>array('username'=>$receiveUser,'name'=>$user['name'],'phone'=>$user['phone'])));
				}else{
					$data=array_merge($data,array('receiveUser'=>array('username'=>$receiveUser)));
				}
				return $data;
			}
		}
		$data=array('id'=>$data['id'],
						'username'=>$data['username'],
						'expressCompany'=>$data['expressCompany'],
						'heavy'=>$data['heavy']+0,
						'big'=>$data['big']+0,
						'isReceived'=>false,
						'depository'=>$data['depository'],
						'destination'=>$data['destination'],
						'remark'=>base64_decode($data['remark']),
						'status'=>$data['status']);
		return $data;
	}
}
?>